﻿@{
    ViewData["Title"] = "菜单管理";
}
	<div class="box2" panelTitle="检索条件">
		<form id="queryForm" method="post">
		<table formMode="transparent">
			<tr>
				<td>菜单名称：</td>
				<td><input type="text" name="menuName" id="menuName"/></td>
                <td><button type="button" onclick="searchHandler()"><span class="icon_find">查询</span></button></td>
                <td><button type="button" onclick="resetSearch()"><span class="icon_reload">重置</span></button></td>
			</tr>
		</table>
		</form>
	</div>

	<div class="padding_right5">
		<div id="maingrid"></div>
    </div>		

<script type="text/javascript">
    var grid;
    function initComplete() {
        grid = $("#maingrid").quiGrid({
            columns: [
	                { display: '菜单名称', name: 'name', align: 'left', width: "15%", isSort: false },
	                { display: '菜单链接', name: 'url', align: 'left', width: "25%" },
	                { display: '菜单描述', name: 'description', align: 'left', width: "10%" },
	                { display: '父级菜单名称', name: 'parentId', align: 'left', width: "15%" },
                    { display: '启用/禁用', name: 'enabled', align: 'left', width: "10%" }
         ],
            url: '/wms/Menu/getMenuListByPage', sortName: 'menu_id', rownumbers: true, checkbox: true,
            height: '100%', width: "100%", percentWidthMode: true,pageSize:10,
            //顶部图标按钮栏
            toolbar:{}
        });
        //载入工具栏图标
        loadToolbar("", grid);
        addDoubleClick("", grid);
    }

    //双击事件
    function onDblClickRow(rowdata, rowid, rowobj) {
        top.Dialog.open({
            URL: "/wms/Menu/editMenu?menuId=" + rowdata.menuId,
            Title: "修改", Width: 600, Height: 500
        });
    }

    //查询
    function searchHandler() {
        //得到查询参数
        var query = $("#queryForm").formToArray();
        //将查询参数传给grid表格
        grid.setOptions({ params: query });
        //页号重置为1
        grid.setNewPage(1);
        //重新加载数据
        grid.loadData();
    }

    //重置查询
    function resetSearch() {
        $("#queryForm")[0].reset();
        searchHandler();
    }

    //添加
    function addMenu() {
        top.Dialog.open({ URL: "/wms/Menu/editMenu", Title: "新增", Width: 600, Height: 500 });
    }

    //批量删除
    function deleteMoreMenu() {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要删除的记录!");
            return;
        }
        top.Dialog.confirm("确定要删除吗？", function () {
            $.post("/wms/Menu/deleteMenu",
            //获取所有选中行
			getSelectId(grid),
			function (result) {
				handleResult(result.status);
			},
			"json");
        });
    }

    //删除后的提示
    function handleResult(result) {
        if (result == -2) {
            showMsg("此菜单有子节点，不能删除！");
        }else if (result == -1) {
            showMsg("删除失败！");
        } else {
            showMsg("删除成功！删除" + result + "条数据");
            grid.loadData();
        }
    }

    //获取所有选中行获取选中行的id 格式为 ids=1&ids=2 
    function getSelectId(grid) {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";

        for (var i = 0; i < selectedRowsLength; i++) {
            ids += selectedRows[i].menuId + ",";
        }
        return { "ids": ids };
    }

    //查看
    function onView(rowid) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要编辑的记录!");
            return;
        } else if (rowsLength > 1) {
            top.Dialog.alert("只能选择一行编辑");
            return;
        }
        rowid = rows[0].menuId;
        top.Dialog.open({ URL: "/wms/Menu/getMenuDetail?menuId=" + rowid, Title: "查看", Width: 600, Height: 560 });
    }

    //修改
    function onEdit(rowid) {
        var rows = grid.getSelectedRows();
        var rowsLength = rows.length;

        if (rowsLength == 0) {
            top.Dialog.alert("请选中要编辑的记录!");
            return;
        } else if (rowsLength > 1) {
            top.Dialog.alert("只能选择一行编辑");
            return;
        }
        rowid = rows[0].menuId;
        top.Dialog.open({
            URL: "/wms/Menu/editMenu?menuId=" + rowid,
            Title: "修改", Width: 600, Height: 500
        });
    }

    //删除
    function onDelete(rowid, rowidx) {
        top.Dialog.confirm("确定要删除该记录吗？", function () {
            //删除记录
            $.post("/wms/Menu/deleteMenu",
	  			{ "ids": rowid },
	  			function (result) {
	  			    handleResult(result.status);
	  			}, "json");
        });
    }

    //刷新表格数据并重置排序和页数
    function refresh(isUpdate) {
        if (!isUpdate) {
            //重置排序
            grid.options.sortName = 'menu_id';
            //页号重置为1
            grid.setNewPage(1);
        }
        grid.loadData();
    }
 
</script>	